﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Specialized;
using System.Configuration;

namespace Core.CMS.Providers
{
    public class SqlContentProvider : ContentProvider
    {
        string connectionString = "";

        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            if (config["connectionString"] != null)
                connectionString = config["connectionString"];
            if (config["connectionStringName"] != null)
                connectionString = ConfigurationManager.ConnectionStrings[config["connectionStringName"]].ConnectionString;

            if (connectionString == null)
                throw new ConfigurationErrorsException("Failed to locate a connection string for the content store.");

            base.Initialize(name, config);
        }

        public override PageInfo GetPage(int PageID)
        { 
            PageInfo result = null;

            using(SqlCommand com = new SqlCommand("GetPage", new SqlConnection(this.connectionString)))
            {
                com.CommandType = System.Data.CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@ID", PageID);
                com.Connection.Open();
                SqlDataReader pageInfoReader = com.ExecuteReader();
                if(pageInfoReader.Read())
                    result = new PageInfo(pageInfoReader);
                com.Connection.Close();
            }

            return result;
        }

        public override int SavePage(PageInfo Info)
        {
            int result = -1;

            using (SqlCommand com = new SqlCommand("SavePage", new SqlConnection(this.connectionString)))
            {
                com.CommandType = System.Data.CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@ID", Info.ID);
                com.Parameters.AddWithValue("@Parent", Info.Parent);
                com.Parameters.AddWithValue("@Title", Info.Title);
                com.Parameters.AddWithValue("@Description", Info.Description);
                com.Parameters.AddWithValue("@Keywords", Info.Keywords);
                com.Connection.Open();
                result = (int)com.ExecuteScalar();
                com.Connection.Close();
            }

            return result;
        }

        public override void DeletePage(int PageID)
        {
            if (PageID != -1)
            {
                using (SqlCommand com = new SqlCommand("DeletePage", new SqlConnection(this.connectionString)))
                {
                    com.CommandType = System.Data.CommandType.StoredProcedure;
                    com.Parameters.AddWithValue("@ID", PageID);
                    com.Connection.Open();
                    com.ExecuteNonQuery();
                    com.Connection.Close();
                }
            }
        }

        public override string GetContent(int PageID, string Name)
        {
            string result = "";

            if (PageID != -1)
            {
                using (SqlCommand com = new SqlCommand("GetContent", new SqlConnection(this.connectionString)))
                {
                    com.CommandType = System.Data.CommandType.StoredProcedure;
                    com.Parameters.AddWithValue("@ID", PageID);
                    com.Parameters.AddWithValue("@Name", Name);
                    com.Connection.Open();
                    object queryresult = com.ExecuteScalar();
                    if (queryresult != DBNull.Value && queryresult != null)
                        result = queryresult.ToString();
                    com.Connection.Close();
                }
            }

            return result;
        }

        public override void SetContent(int PageID, string Name, string Content)
        {
            if (PageID != -1)
            {
                using (SqlCommand com = new SqlCommand("SetContent", new SqlConnection(this.connectionString)))
                {
                    com.CommandType = System.Data.CommandType.StoredProcedure;
                    com.Parameters.AddWithValue("@ID", PageID);
                    com.Parameters.AddWithValue("@Name", Name);
                    com.Parameters.AddWithValue("@Content", Content);
                    com.Connection.Open();
                    com.ExecuteNonQuery();
                    com.Connection.Close();
                }
            }
            else
                throw new InvalidOperationException("Cannot save content without a first creating the page.");
        }

        public override List<PageInfo> GetChildren(int Parent)
        {
            List<PageInfo> results = new List<PageInfo>();

            using (SqlCommand com = new SqlCommand("GetChildren", new SqlConnection(this.connectionString)))
            {
                com.CommandType = System.Data.CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@Parent", Parent);
                com.Connection.Open();
                SqlDataReader childReader = com.ExecuteReader();
                while (childReader.Read())
                {
                    results.Add(new PageInfo(childReader));
                }
                com.Connection.Close();
            }

            return results;
        }
    }
}
